<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateStatisticsAPIRequest;
use App\Http\Requests\API\UpdateStatisticsAPIRequest;
use App\Models\Statistics;
use App\Repositories\StatisticsRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class StatisticsController
 * @package App\Http\Controllers\API
 */

class StatisticsAPIController extends AppBaseController
{
    /** @var  StatisticsRepository */
    private $statisticsRepository;

    public function __construct(StatisticsRepository $statisticsRepo)
    {
        $this->statisticsRepository = $statisticsRepo;
    }

    /**
     * Display a listing of the Statistics.
     * GET|HEAD /statistics
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->statisticsRepository->pushCriteria(new RequestCriteria($request));
        $this->statisticsRepository->pushCriteria(new LimitOffsetCriteria($request));
        $statistics = $this->statisticsRepository->all();

        return $this->sendResponse($statistics->toArray(), 'Statistics retrieved successfully');
    }

    /**
     * Store a newly created Statistics in storage.
     * POST /statistics
     *
     * @param CreateStatisticsAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateStatisticsAPIRequest $request)
    {
        $input = $request->all();

        $statistics = $this->statisticsRepository->create($input);

        return $this->sendResponse($statistics->toArray(), 'Statistics saved successfully');
    }

    /**
     * Display the specified Statistics.
     * GET|HEAD /statistics/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var Statistics $statistics */
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            return $this->sendError('Statistics not found');
        }

        return $this->sendResponse($statistics->toArray(), 'Statistics retrieved successfully');
    }

    /**
     * Update the specified Statistics in storage.
     * PUT/PATCH /statistics/{id}
     *
     * @param  int $id
     * @param UpdateStatisticsAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateStatisticsAPIRequest $request)
    {
        $input = $request->all();

        /** @var Statistics $statistics */
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            return $this->sendError('Statistics not found');
        }

        $statistics = $this->statisticsRepository->update($input, $id);

        return $this->sendResponse($statistics->toArray(), 'Statistics updated successfully');
    }

    /**
     * Remove the specified Statistics from storage.
     * DELETE /statistics/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Statistics $statistics */
        $statistics = $this->statisticsRepository->findWithoutFail($id);

        if (empty($statistics)) {
            return $this->sendError('Statistics not found');
        }

        $statistics->delete();

        return $this->sendResponse($id, 'Statistics deleted successfully');
    }

    public function distributionStatistics()
    {
        $user_id = Auth::id();
        $day = $this->statisticsRepository->distributionStatistics(1,$user_id);
        $month = $this->statisticsRepository->distributionStatistics(2,$user_id);
        return responseZK(1,['day'=>$day, 'month'=>$month]);
    }
}
